package test.test.one.levels;

import test.test.one.R;
import test.test.one.TestActivity;
import test.test.one.objects.ob_exit;
import test.test.one.objects.ob_pickable;
import test.test.one.objects.tools.Speed;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

//TODO missing a lot of documentation
public class level1 extends AbstractLevel implements SurfaceHolder.Callback {
	
	/*
	 * first is ALWAYS TAG
	 */
	
	private static final String TAG = level1.class.getSimpleName();

	
	/*
	 * variable declaration
	 */
	private ob_exit bt_next;
	private ob_pickable item01,item02;
	private boolean dir_rigth=true;
	
	private int cord=60;

	//private final int inv=2;
	//public boolean levelIsOver=false;
	//public AbstractObject lvl_objects[] = {player, bt_exit, bt_next};
	
	
	public level1(Context context){
		super(context);
		Log.d(TAG, "lvl1 created");
		
		//player.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.super_mario_2));
		this.bt_next=new ob_exit(BitmapFactory.decodeResource(getResources(), R.drawable.x), 20, 50);
		this.item01= new ob_pickable(BitmapFactory.decodeResource(getResources(), R.drawable.sandwich), 
				TestActivity.GAMEWIDTH/3, TestActivity.GAMEHEIGHT/2);
		this.item02= new ob_pickable(BitmapFactory.decodeResource(getResources(), R.drawable.sandwich), 
				2*TestActivity.GAMEWIDTH/3, TestActivity.GAMEHEIGHT/2);
		new_pos=player.getX();
	}
	
	public boolean onTouchEvent(MotionEvent event){
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			
			//handle all objects
			
			//for(int i=0;i<lvl_objects.length;i++);
				//lvl_objects[i].handleActionDown((int)event.getX(), (int)event.getY());
			
			player.handleActionDown((int)event.getX(), (int)event.getY());
			bt_exit.handleActionDown((int)event.getX(), (int)event.getY());
			bt_next.handleActionDown((int)event.getX(), (int)event.getY());
			item01.handleActionDown((int)event.getX(), (int)event.getY());
			item02.handleActionDown((int)event.getX(), (int)event.getY());
			bag.handleActionDown((int)event.getX(), (int)event.getY());
			
			if(item01.isTouched()||item02.isTouched()){
			
				if(item01.isTouched()&&!item01.getIsPicked()&&
						(Math.abs(player.getX()-item01.getX())<=100)){
					
					
					//item01.setSpeed(new Speed (0,3));
					player.addItem(item01);
					item01.setIsPicked(true);
					item01.setX(cord+=50);
					item01.setY(30);
					
					
					Log.d(TAG, "item01 is added to inventory, on position "+ (player.item_counter -1));
					
				}
				
				if(item02.isTouched()&&!item02.getIsPicked()&&
						(Math.abs(player.getX()-item02.getX())<=50)){
					
					//item02.setSpeed(new Speed (0,3));
					player.addItem(item02);
					item02.setIsPicked(true);
					item02.setX(cord+=50);
					item02.setY(30);
					
					Log.d(TAG, "item02 is added to inventory, on position "+ (player.item_counter-1));
					
				}
				return true;
			}
			
			if (bag.isTouched()){
				player.inventoryIsOpen=!player.inventoryIsOpen;
			}
			
			if (bt_next.isTouched()){
				Log.d(TAG,"next is touched");
				thread.setRunning(false);
				((Activity) getContext()).setContentView(TestActivity.level2);
			}
			
			if (bt_exit.isTouched()){
				thread.setRunning(false);
				((Activity)getContext()).finish();
			}
			else{
				Log.d(TAG, "x=" + event.getX() + ",y=" + event.getY());
				if(event.getY()>(TestActivity.GAMEHEIGHT-(player.getBitmap().getHeight()))){
					new_pos=event.getX();
					player.setSpeed(new Speed(2,0));
					dir_rigth=true;
					
					if(new_pos<player.getX()){
						player.getSpeed().toggleXDirection();
						dir_rigth=false;
					}
				}
			}
		}
		return true;
	}
	
	public void render(Canvas canvas) {
		super.render(canvas);
		
		//canvas.drawBitmap(background,0,0,null);		
		//bt_exit.draw(canvas);
		//player.draw(canvas);
		//bag.draw(canvas);
		
		bt_next.draw(canvas);
		
		if (!item01.getIsPicked())
			item01.draw(canvas);	
		
		if (!item02.getIsPicked())
			item02.draw(canvas);	
		
		
		
		if (player.inventoryIsOpen){
			bag.display(canvas);
			if(item01.getIsPicked())
				item01.draw(canvas);
			if(item02.getIsPicked())
				item02.draw(canvas);
			
		}
	}
	
	public void update() {
		
		if(player.getX()<new_pos+5 && player.getX()>new_pos-5){
			player.setSpeed(new Speed());
			
			if(!dir_rigth)
				player.getSpeed().toggleXDirection();
			//if(mainCharacter.getX()>new_pos+5)
			//else
				//mainCharacter.getSpeed().setxDirection(1);
			
		}
		
		player.update(System.currentTimeMillis(), new_pos);

		//player.update();

		
		//if(player.getX()<new_pos+3 && player.getX()>new_pos-3){
			//player.setSpeed(new Speed());
		//}
		
		//if (item01.getY()<=30){
			//item01.setSpeed(new Speed ());
			//player.addItem(item01);
			//item01.setIsPicked(true);
			//item01.setX(cord+=50);
			//item01.setY(30);
		//}
		
		//if (item02.getY()<=30){
			//item02.setSpeed(new Speed ());
			//player.addItem(item02);
			//item02.setIsPicked(true);
			//item02.setX(cord+=50);
			//item02.setY(30);
		//}
		
		//player.update();
		//item01.update();
		//item02.update();
	
	}

}
